<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Exportorder extends CI_Controller {

	/**
	 * 
	 *  自动导单程序 
	 *
	 */
	function __construct()
	 {
		 parent::__construct();
		 $this->load->model('Function_m','fun');
		 $this->load->model('Exportorder_m','m');
		 $this->pageSize =$this->config->item('pagenum');
	 }
	 
	function ajaxbutton()
	{
		$op = $this->input->get_post('op');
		
		/******************************************************************
			**  导出亚马逊订单 【马帮抓取亚马逊接口版本】
			**  liuxiang 2017 5 23
			** library Amazondata.php 
		****************************************************************/
		if($op =='amazonorderlist')	
		{
			$this->load->library('Amazondata');
			$ress = $this->amazondata->getinsertorderlist();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		/******************************************************************
			**  系统完成已发货功能 【新系统抓取亚马逊接口版本】
			**  liuxiang 2017 7.06 将亚马逊所有已发货的订单上传到远程123数据库
			** library UpdateAmazondataPro.php 
		****************************************************************/
		if($op =='UpdateAmazondata')	
		{
			$this->load->library('Amazondata');
			$ress = $this->amazondata->UpdateAmazondata();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		/******************************************************************
			**  导出亚马逊订单 【新系统抓取亚马逊接口版本】
			**  liuxiang 2017 6.23 于上面同属抓取亚马逊订单功能
			** library AmazondataPro.php 
		****************************************************************/
		if($op =='amazonorderlistPro')	
		{
			$this->load->library('AmazondataPro');
			$ress = $this->amazondatapro->getinsertorderlist();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		/******************************************************************
			**  导出亚马逊FBA订单 【新系统抓取亚马逊接口版本】
			**  liuxiang 2017 7.25 
			** library AmazondataFBA.php 
		****************************************************************/
		if($op =='amazonorderlistFBA')	
		{
			$this->load->library('AmazondataFBA');
			$ress = $this->amazondatafba->getinsertorderlist();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		/******************************************************************
			**  导出亚马逊FBA订单的退款信息 【新系统抓取亚马逊接口版本】
			**  liuxiang 2017 10.11 
			** library AmazondataFBA.php 
		****************************************************************/
		if($op =='amzorder_fba_refundlist')
		{
			$this->load->library('AmazondataFBA');
			$ress = $this->amazondatafba->amzorder_fba_refundlist();
			$this->amazondatafba->getFBAfulflllment();//亚马逊物流已收到库存报告
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		/******************************************************************
			**  读取FBA所有库存
			**  liuxiang 2017 11.03 
			** library AmazondataFBA.php 
		****************************************************************/
		if($op =='getFBAstock')
		{
			$this->load->library('AmazondataFBA');
			$ress = $this->amazondatafba->getFBAstock();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		
		/******************************************************************
			**  导出亚马逊FBA订单 【新系统抓取原马帮中亚马逊Fba】
			**  liuxiang 2017 8.17 
			** library AmazondataFBA.php 
		****************************************************************/
		if($op =='MabangrderlistFBA')	
		{
			$this->load->library('AmazondataFBA');
			//$ress = $this->amazondatafba->getMabanginsertFBAorderlist();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
	    /******************************************************************
			**  导出亚马逊取消的订单
			**  liuxiang 2017 11.14 
			** library AmazondataFBA.php 
		****************************************************************/
		if($op == 'amazonordercancel')
		{
			$this->load->library('AmazondataFBA');
			$ress = $this->amazondatafba->amazonordercancel();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		 /******************************************************************
			**  导出亚马逊所有在售的SKU
			**  liuxiang 2018 02.27 
			** library AmazondataFBA.php 
		****************************************************************/
		if($op == 'getamazongoodslist')
		{
			$this->load->library('AmazondataFBA');
			$ress = $this->amazondatafba->getgoodslist();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		/******************************************************************
			**  系统完成已发货功能 【新系统抓取亚马逊接口版本】
			**  liuxiang 2017 7.06 将亚马逊所有已发货的订单上传到远程123数据库
			** library UpdateAmazondataPro.php 
		****************************************************************/
		if($op =='UpdateAmazondataPro')	
		{
			$this->load->library('AmazondataPro');
			$ress = $this->amazondatapro->UpdateAmazondataPro();
			if($ress == false)	//执行完成
			{
				echo "OK";
				die;
			}
		}
		/******************************************************************
			**  库存预警基础信息存储 
			**  liuxiang 2017 7.28 
			** library stockwarning.php 34375条
		****************************************************************/
		if($op == 'stockwarning')
		{
			
			$this->load->library('Stockwarning');
			$this->stockwarning->getallsale();   //直发销量
			$this->stockwarning->getFBAallsale();//FBA销量
			$ress = $this->stockwarning->dailysales();//直发和FBA日销量
			$this->stockwarning->getoversea_transportlist();//海外FBA头程计算数据提取
		
			
			
		}
		/*****************************************************
		** 自动匹配物流信息
		** 需要定时执行程序，根据订单搜索脚本自动匹配订单的物流信息
		********************************************************/
		if($op =='autoorderexpress')
		{
			$this->load->library('Orderexpress');
			$ress = $this->orderexpress->getexpressorderlist();
			$ress = $this->orderexpress->changorderstatus();	//更新是否超重信息
		}
		/*****************************************************
		** 自动判断是否缺货状态
		** 需要定时执行程序，根据订单搜索匹配当前是否为缺货状态
		********************************************************/
		if($op =='isquehuo')
		{
			$this->load->library('Orderexpress');

			//$rs = $this->orderexpress->isquehuotest();

			$rs = $this->orderexpress->doSeparateWarehouse();//更新后的分仓规则

			
		}
		/*****************************************************
		 ** 自动判断是否分仓
		 ** 需要定时执行程序，根据订单搜索匹配当前是否为分仓状态
		 ********************************************************/
		if($op =='doSeparateWarehouse')
		{
			$this->load->library('Orderexpress');

			$rs = $this->orderexpress->doSeparateWarehouse();


		}

		if($op =='checkmabangOrder')
		{
			 /***************************
			
			 ** 新系统切订单的时候，判断一下订单是否存在在马帮。
			 ** 订单切完之后，可以取消这个功能
			 *********************************/
			if($this->config->item('isupdatemabang')==1)
			{
				$this->load->library('Mabang');
				$this->mabang->checkmabangOrder();
			}
			 /*******同步商品库存到马帮结束***********/

		}
		/*****************************************************
		** 自动将订单从已发货改成已完成
		********************************************************/
		if($op =='isendorder')
		{
			$this->load->library('Orderexpress');
			$rs = $this->orderexpress->isendorder();
		}
		/*****************************************************
		 **
		 ** 获取海外仓已发货的运单号信息
		 ********************************************************/
		if($op =='getDeliverNumberOversea')
		{
			$this->load->library('Orderexpress');
			//$ress = $this->orderexpress->isquehuo();
			$rs = $this->orderexpress->getDeliverNumberOversea();
		}

		/*****************************************************
		 ** 给相关人员发送系统提醒消息，下架，上架，清仓等
		 ********************************************************/
		if($op =='autoSendMessage')
		{
			$this->message->sysstatisticalMessage();
		}
		/*****************************************************
		** 同步马帮现有的库存信息
		********************************************************/
		if($op =='updatemabangstock')
		{
			if($this->config->item('isupdatemabang')==1)
			{
				$pageid = $this->input->get('pageid');
				$nowpage = $pageid;
				if ($nowpage == "")
					$nowpage = 1;
				else
					$nowpage = $nowpage+1;

				//$this->load->library('Mabang');
				//$res = $this->mabang->updatemabangstock($nowpage);
				if($res)
				{
					//echo "<meta http-equiv=\"refresh\" content=\"1; url=/exportorder/ajaxbutton?op=updatemabangstock&pageid=$nowpage\">";
				}
			}
		}

	
		

		
		/*****************************************************
		** wisth 导单分四步
		1、 requestdownloadjob
		2、seconddownloadjob
		3、thirdstep
		4、fourthstep
		** 执行1后，等待10分钟再执行2
		//之后可以取消下面的本地执行的wish
		********************************************************/

		if($op =='wishorderlistPro')	
		{
			$this->load->library('WishdataPro');
			$ress = $this->wishdatapro->requestdownloadjob();
			
		}
		if($op =='seconddownloadjob')
		{
			$this->load->library('WishdataPro');
			$ress = $this->wishdatapro->seconddownloadjob();
		}
		if($op =='thirdstep')
		{
			$this->load->library('WishdataPro');
			$ress = $this->wishdatapro->thirdstep();
		}
		if($op =='fourthstep')
		{
			$this->load->library('WishdataPro');
			$ress = $this->wishdatapro->fourthstep();
		}
		//wissh 更新接口
		if($op =='wishrefreshtoken')	
		{
			$id = $this->input->get_post('id');
			// $id=143;
			// $id=144;
			$this->load->library('Wishrefreshtoken');
		    $this->wishrefreshtoken->getalltoken($id);
		}
		if($op =='wishreturn')	
		{
			//回传交运单号
		     // $from = $this->input->get_post('from');
		     // $to=$this->input->get_post('to');
			$this->load->library('Wishreturn');
		    $this->wishreturn->returne();

		}
		/************ wish Pro 结束********************************/
		

		/************ 处理123上远程速卖通订单开始********************************/
//			if($op =='aliexpressorderlistPro')
//			{
//				$this->load->library('AliexpressdataPro');
//				$rs = $this->aliexpressdatapro->doInsertOrderData();
//			}
//			if($op=='createSendGoodsOrderOnlinePro')
//			{
//				$this->load->library('AliexpressdataPro');
//				$rs = $this->aliexpressdatapro->createSendGoodsOrderOnline();
//			}

		/************ 处理123上远程速卖通订单结束********************************/
		

		//速卖通订单
		if($op =='aliexpressorderlist')
		{
			$this->load->library('Aliexpressdata');
			$rs = $this->aliexpressdata->doInsertOrderRecords();
		}
		//获取速卖通所有店铺的所有商品
		/*
		if($op == 'aliexpressgoodslist'){
			$this->load->library('Aliexpressdata');
			$rs = $this->aliexpressdata->getAliexpressGoodsList();
		}*/

		//速卖通无忧交运开始
		if($op =='aliexpresswuliu')
		{
			$this->load->library('Aliexpressdata');
			$rs = $this->aliexpressdata->aliexpresswuliu();
			if($rs)
			{
				echo $rs;
				echo "<meta http-equiv=\"refresh\" content=\"1; url=/exportorder/ajaxbutton?op=aliexpresswuliu \">";
			}
		}
		if($op =='aliexpresswuliuTwo')
		{
			$this->load->library('Aliexpressdata');
			$rs = $this->aliexpressdata->aliexpresswuliuTwo();
			if($rs)
			{
				echo $rs;
				echo "<meta http-equiv=\"refresh\" content=\"1; url=/exportorder/ajaxbutton?op=aliexpresswuliuTwo \">";
			}
		}
		//速卖通无忧交运结束


		//速卖通订单 运单号回传
		if($op =='createSendGoodsOrderOnline')
		{
			$this->load->library('Aliexpressdata');
			$rs = $this->aliexpressdata->createSendGoodsOrderOnline();
		}



			/********************************************/
		if($op =='ebayorderlist')	
		{
		   $id = $this->input->get_post('id');
			$this->load->library('Ebaydata');
		    $this->ebaydata->getebaytoken($id);
		   // var_dump($res);
		    $this->ebaydata->checksku();
			// $ress = $this->wishdata->getno();
		}

		//获取paypal账户交易记录
		if($op=='paypalnotes'){
			echo 1;
			$this->load->library('PaypalRefund');
			echo 2;
			$this->paypalrefund->getpaypalnotes();
			echo 3;
		}


		if($op =='magentoorderlist')	
		{
			echo 1;
		     // $from = $this->input->get_post('from');
		     // $to=$this->input->get_post('to');
			$this->load->library('Magentodata');
			echo 2;
			
		    $this->magentodata->getdata();
			echo 3;
		}

		if($op == 'magento_v1_shopify'){
			$webname = $this->input->get_post('webname');
			//echo $webname;
			$this->load->library('Magentodata');
			$this->magentodata->magento_v1_shopify($webname);
		}
        
        //shopee 导单程序
		if($op =='shopeeorderlist')	
		{
			echo 1;
		     // $from = $this->input->get_post('from');
		     // $to=$this->input->get_post('to');
			$this->load->library('Shopeedata');
			echo 2;
			
		     var_export($this->shopeedata->start());
			echo 3;
		}
        
        //shopee退款
		if($op == 'shopeereturnlist'){
          $this->load->library('Shopeedata');
          var_export($this->shopeedata->returnlist('9602437','273'));
		}

		//shopee商品下载
		if($op == 'shopeeproductlist'){
			$this->load->library('Shopeedata');
			$shopid=$this->input->get_post('shopid');
			var_export($this->shopeedata->productlist($shopid));
		}

		//shopify gettoken
		if($op == 'shopifytoken'){
			$shopid=$this->input->get_post('shopid');
			$code=$this->input->get_post('code');
			$this->load->library('Shopifydata');
			$this->shopifydata->gettoken($shopid,$code);

		}
        
        //shopify 订单 退款 发货
		if($op == 'shopifyorderlist'){
			$this->load->library('Shopifydata');
			$this->shopifydata->getorder();
		}
		
		//shopify 产品
		if($op == 'shopifyproduct'){
		  $id = $this->input->get_post('id');
          $this->load->library('Shopifydata');
          $this->shopifydata->getnetproduct($id);
		}

		//shopify googleshipping
		if($op == 'shopifygoogleshipping'){
			$id = $this->input->get_post('id');
			$this->load->library('Shopifydata');
			$this->shopifydata->googleshipping($id);
		}

		if($op =='magentoorderreturn')	
		{
		     // $from = $this->input->get_post('from');
		     // $to=$this->input->get_post('to');
			$this->load->library('Magentoreturn');
		    $this->magentoreturn->returne();

		}
		if($op =='ebayreturn')	
		{
			 //$id = $this->input->get_post('id');
		     // $from = $this->input->get_post('from');
		     // $to=$this->input->get_post('to');
			$this->load->library('Ebayreturn');
		    $this->ebayreturn->returne();

		}
		/**********************
		** /oversea/deliveryplanpro
		** 库存需求列表 核实可发数 每天定时清0
		**************************/
		if($op =='overseaverifyNums')	
		{
			$this->db->update("t_overseapurchasing",array("verifyNums"=>0,"verify_tip"=>0),"demandID >0 and status = 0");
		}

		//批量订单列表 在途数据更新每日
		if($op=="updBatchOrderCount"){
			$this->load->library('Orderexpress');
			$this->orderexpress->updBatchOrderCount();
		}
		//每日统计 总库存数 以及 总库存价值
		if($op=="getStockCountsPerDay"){
			$this->load->library('Orderexpress');
			$this->orderexpress->getStockCountsPerDay();
		}
		//定期执行 计算正向开发提成
		if($op=="Profit2newProduct"){
			$this->load->library('Orderexpress');
			$this->orderexpress->Profit2newProduct();
		}
		//每日执行，提醒各销售5天内订单的亏本情况
		if($op == "sendMessageToSales"){
			$this->load->library('Orderexpress');
			$this->orderexpress->sendMessageToSales();
		}
		//销售达指定数量的商品提醒设置相关子sku信息 发送信息给刘艺 李明道
		if($op == "sendMessageBySalesCount"){
			$this->load->library('Orderexpress');
			$this->orderexpress->sendMessageBySalesCount();
		}

		//核算外采购商品库息
		if($op == "getStockInterestPerDay"){
			$this->load->library('Orderexpress');
			$this->orderexpress->getStockInterestPerDay();
			$this->orderexpress->getStockInterestPerDayFBA();
		}

		//将商品销售日均达一定数量的商品自动标为重要商品
		if($op == "setImportantGoods"){
			$this->load->library('Orderexpress');
			$this->orderexpress->setImportantGoods();
		}

		//获取1688订单物流等相关信息
		if($op == 'alibabalogistics'){
			$this->load->library('Alibabadata');
			$rs = $this->alibabadata->getAlibabaLogisticsList();
		}

		//获取速卖通聚石塔内数据
		if($op == 'aliexpresstowerapi'){
			$this->load->library('AliexpressTowerApi');
			$rs = $this->aliexpresstowerapi->getOrderList();
		}
		//定期执行临时表数据更新到正式表
		if($op == 'insertOrderListData'){
			$this->load->library('AliexpressTowerApi');
			$rs = $this->aliexpresstowerapi->insertOrderListData();
		}
		//更新相关运单号到店铺
		if($op == 'updShippingNumberOrderOnline'){
			$this->load->library('AliexpressTowerApi');
			$rs = $this->aliexpresstowerapi->updShippingNumberOrderOnline();
		}



	}


}
